package com.github.catvod.spider;

import android.text.TextUtils;

import com.github.catvod.bean.Class;
import com.github.catvod.bean.Filter;
import com.github.catvod.bean.Result;
import com.github.catvod.bean.Vod;
import com.github.catvod.crawler.Spider;
import com.github.catvod.net.OkHttp;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

public class Fjvtw extends Spider {

    private final String url = "https://fjvtw.com";

    private Filter getFilter(String name, String key, Elements typeList) {
        List<Filter.Value> values = new ArrayList<>();

        if ("type".equals(key)) {
            values.add(new Filter.Value("全部", ""));
            for (Element type : typeList) {
                switch (type.text()) {
                    case "喜剧":
                        values.add(new Filter.Value("喜剧", "/type/22"));
                        break;
                    case "动作":
                        values.add(new Filter.Value("动作", "/type/23"));
                        break;
                    case "科幻":
                        values.add(new Filter.Value("科幻", "/type/30"));
                        break;
                    case "爱情":
                        values.add(new Filter.Value("爱情", "/type/26"));
                        break;
                    case "悬疑":
                        values.add(new Filter.Value("悬疑", "/type/27"));
                        break;
                    case "奇幻":
                        values.add(new Filter.Value("奇幻", "/type/87"));
                        break;
                    case "剧情":
                        values.add(new Filter.Value("剧情", "/type/37"));
                        break;
                    case "恐怖":
                        values.add(new Filter.Value("恐怖", "/type/36"));
                        break;
                    case "犯罪":
                        values.add(new Filter.Value("犯罪", "/type/35"));
                        break;
                    case "动画":
                        values.add(new Filter.Value("动画", "/type/33"));
                        break;
                    case "惊悚":
                        values.add(new Filter.Value("惊悚", "/type/34"));
                        break;
                    case "战争":
                        values.add(new Filter.Value("战争", "/type/25"));
                        break;
                    case "冒险":
                        values.add(new Filter.Value("冒险", "/type/31"));
                        break;
                    case "灾难":
                        values.add(new Filter.Value("灾难", "/type/81"));
                        break;
                    case "伦理":
                        values.add(new Filter.Value("伦理", "/type/83"));
                        break;
                    case "其他":
                        values.add(new Filter.Value("其他", "/type/43"));
                        break;
                    case "国产剧":
                        values.add(new Filter.Value("国产剧", "/type/14"));
                        break;
                    case "欧美剧":
                        values.add(new Filter.Value("欧美剧", "/type/15"));
                        break;
                    case "港台剧":
                        values.add(new Filter.Value("港台剧", "/type/16"));
                        break;
                    case "日韩剧":
                        values.add(new Filter.Value("日韩剧", "/type/62"));
                        break;
                    case "其他剧":
                        values.add(new Filter.Value("其他剧", "/type/68"));
                        break;
                    case "国产综艺": values.add(new Filter.Value("国产综艺", "type/69")); break;
                    case "港台综艺": values.add(new Filter.Value("港台综艺", "type/70")); break;
                    case "日韩综艺": values.add(new Filter.Value("日韩综艺", "type/72")); break;
                    case "欧美综艺": values.add(new Filter.Value("欧美综艺", "type/73")); break;
                    case "国产动漫": values.add(new Filter.Value("国产动漫", "type/75")); break;
                    case "日韩动漫": values.add(new Filter.Value("日韩动漫", "type/76")); break;
                    case "欧美动漫": values.add(new Filter.Value("欧美动漫", "type/77")); break;
                }
            }
        }


        if ("area".equals(key)) {
            values.add(new Filter.Value("全部", ""));
            values.add(new Filter.Value("中国大陆", "/area/中国大陆"));
            values.add(new Filter.Value("中国香港", "/area/中国香港"));
            values.add(new Filter.Value("中国台湾", "/area/中国台湾"));
            values.add(new Filter.Value("美国", "/area/美国"));
            values.add(new Filter.Value("日本", "/area/日本"));
            values.add(new Filter.Value("韩国", "/area/韩国"));
            values.add(new Filter.Value("印度", "/area/印度"));
            values.add(new Filter.Value("泰国", "/area/泰国"));
            values.add(new Filter.Value("其他", "/area/其他"));
        }

        if ("year".equals(key)) {
            values.add(new Filter.Value("全部", ""));
            values.add(new Filter.Value("2024", "/year/2024"));
            values.add(new Filter.Value("2023", "/year/2023"));
            values.add(new Filter.Value("2022", "/year/2022"));
            values.add(new Filter.Value("2021", "/year/2021"));
            values.add(new Filter.Value("2020", "/year/2020"));
            values.add(new Filter.Value("2019", "/year/2019"));
            values.add(new Filter.Value("2018", "/year/2018"));
            values.add(new Filter.Value("2017", "/year/2017"));
            values.add(new Filter.Value("2016", "/year/2016"));
            values.add(new Filter.Value("2015", "/year/2015"));
            values.add(new Filter.Value("2014", "/year/2014"));
            values.add(new Filter.Value("2013", "/year/2013"));
            values.add(new Filter.Value("2012", "/year/2012"));
            values.add(new Filter.Value("2011", "/year/2011"));
            values.add(new Filter.Value("2010", "/year/2010"));
            values.add(new Filter.Value("2009~2000", "/year/2009~2000"));
        }

        if ("by".equals(key)) {
            values.add(new Filter.Value("全部", ""));
            values.add(new Filter.Value("上映时间↑", "/sortType/1/sortOrder/1"));
            values.add(new Filter.Value("上映时间↓", "/sortType/1/sortOrder/0"));
            values.add(new Filter.Value("人气高低↑", "/sortType/3/sortOrder/1"));
            values.add(new Filter.Value("人气高低↓", "/sortType/3/sortOrder/0"));
            values.add(new Filter.Value("评分高低↑", "/sortType/4/sortOrder/1"));
            values.add(new Filter.Value("评分高低↓", "/sortType/4/sortOrder/0"));
        }
        return new Filter(key, name, values);
    }

    @Override
    public String homeContent(boolean filter) throws Exception {
        List<Class> classes = new ArrayList<>();
        List<Vod> list = new ArrayList<>();
        LinkedHashMap<String, List<Filter>> filters = new LinkedHashMap<>();
        Document doc = Jsoup.parse(OkHttp.string(url));
        for (Element a : doc.select("div.eQYEYa")) {
            String typeName = a.select("div.name").text();
            String typeId = a.select("a").attr("href");
            Elements typeList = a.select("div.eVtwKa").select("a");
            classes.add(new Class(typeId, typeName));
            List<Filter> array = new ArrayList<>();
            array.add(getFilter("类型", "type", typeList));
            array.add(getFilter("年份", "year", null));
            array.add(getFilter("地区", "area", null));
            array.add(getFilter("排序", "by", null));
            filters.put(typeId, array);
        }
        for (Element div : doc.select("div.content-card")) {
            String id = div.select("a").attr("href");
            String name = div.select("div.title > span").get(0).text();
            String nameStr = name;
            if (nameStr.contains("(")) nameStr = nameStr.replace("(", "\\(");
            if (nameStr.contains(")")) nameStr = nameStr.replace(")", "\\)");
            String pic = "";
            try {
                String[] val = doc.select("script").toString().split(nameStr);
                if (!val[1].contains("http") && val.length == 3) {
                    pic = val[2].split("https://")[1].split("\\\\\"")[0];
                } else {
                    pic = val[1].split("https://")[1].split("\\\\\"")[0];
                }
            } catch (Exception e) {
                pic = "";
            }
            String tag = div.select(".tag").text();
            String remark = div.select("div.bottom > div").get(0).text();
            Vod vod = new Vod(id, name, "https://" + pic, tag + (remark.isBlank() ? "" : "/") + remark);;
            list.add(vod);
        }
        return Result.string(classes, list, filters);
    }

    @Override
    public String categoryContent(String tid, String pg, boolean filter, HashMap<String, String> extend) throws Exception {
        List<Vod> list = new ArrayList<>();
        if (extend.isEmpty()) {
            extend = new HashMap<>();
        }
        if (!extend.containsKey("type")) {
            extend.put("type", "");
        }
        if (!extend.containsKey("year")) {
            extend.put("year", "");
        }
        if (!extend.containsKey("area")) {
            extend.put("area", "");
        }
        if (!extend.containsKey("by")) {
            extend.put("by", "");
        }
        String target = pg.equals("1") ? url + tid.concat(extend.get("type")).concat(extend.get("year")).concat(extend.get("area")).concat(extend.get("by")) : url + tid + "/page/" + pg.concat(extend.get("type")).concat(extend.get("year")).concat(extend.get("area")).concat(extend.get("by"));
        Document doc = Jsoup.parse(OkHttp.string(target));
        for (Element div : doc.select(".content-card")) {
            String id = div.select("a").attr("href");
            String name = div.select("div.title").get(0).text();
            String nameStr = name;
            if (nameStr.contains("(")) nameStr = nameStr.replace("(", "\\(");
            if (nameStr.contains(")")) nameStr = nameStr.replace(")", "\\)");
            String pic;
            try {
                String[] val = doc.select("script").toString().split(nameStr);
                if (!val[1].contains("http") && val.length == 3) {
                    pic = val[2].split("https://")[1].split("\\\\\"")[0];
                } else {
                    pic = val[1].split("https://")[1].split("\\\\\"")[0];
                }
            } catch (Exception e) {
                pic = "";
            }
            String tag = div.select(".tag").text();
            String remark = div.select("div.bottom > div").get(0).text();
            Vod vod = new Vod(id, name, "https://" + pic, tag + (remark.isBlank() ? "" : "/") + remark);
            list.add(vod);
        }
        return Result.string(list);
    }

    @Override
    public String detailContent(List<String> ids) throws Exception {
        String html = OkHttp.string(url + ids.get(0));
        Document doc = Jsoup.parse(html);
        String name = doc.select("h1.title").text();
        String nameStr = name;
        if (nameStr.contains("(")) nameStr = nameStr.replace("(", "\\(");
        if (nameStr.contains(")")) nameStr = nameStr.replace(")", "\\)");
        String pic;
        try {
            String[] val = doc.select("script").toString().split(nameStr);
            if (!val[1].contains("http") && val.length == 3) {
                pic = val[2].split("https://")[1].split("\\\\\"")[0];
            } else {
                pic = val[1].split("https://")[1].split("\\\\\"")[0];
            }
        } catch (Exception e) {
            pic = "";
        }
        Elements sourceList = doc.select("div.listitem").select("a");
        List<String> vodItems = new ArrayList<>();
        for (Element a : sourceList) {
            String episodeUrl = a.select("a").attr("href");
            String episodeName = a.select("a").text();
            vodItems.add(episodeName + "$" + url + episodeUrl);
        }
        Vod vod = new Vod();
        vod.setTypeName(doc.select(".tags").text());
        vod.setVodId(ids.get(0));
        vod.setVodDirector(doc.select(".director").get(0).select("a").text());
        vod.setVodActor(doc.select(".director").get(1).select("a").text());
        vod.setVodPic("https://" + pic);
        vod.setVodName(name);
        vod.setVodPlayFrom("金牌影院");
        vod.setVodContent(doc.select("div.wrapper_more_text").text());
        vod.setVodPlayUrl(TextUtils.join("#", vodItems));
        vod.setVodArea(doc.select("div.iovnOD .item").get(1).select("div.item-top").text());
        vod.setVodYear(doc.select("div.iovnOD .item").get(2).select("div.item-top").text());
        vod.setVodRemarks("评分：" + doc.select("div.iovnOD .item").get(0).select("div.item-top").text());
        return Result.string(vod);
    }

    @Override
    public String playerContent(String flag, String id, List<String> vipFlags) throws Exception {
        return Result.get().url(id).parse().click("document.getElementsByClassName('xgplayer-start')[0].click()").string();
    }

    @Override
    public boolean manualVideoCheck() throws Exception {
        return true;
    }

    @Override
    public boolean isVideoFormat(String url) throws Exception {
        return !url.contains("afcdn.net") && url.contains(".m3u8");
    }

    @Override
    public String searchContent(String key, boolean quick) throws Exception {
        return searchContent("/vod/search/" + key);
    }

    @Override
    public String searchContent(String key, boolean quick, String pg) throws Exception {
        return searchContent("/vod/search/" + key);
    }

    private String searchContent(String query) {
        List<Vod> list = new ArrayList<>();
        Document doc = Jsoup.parse(OkHttp.string(url + query));
        for (Element div : doc.select("div.content-card")) {
            String id = div.select("a").attr("href");
            String name = div.select("div.title").get(0).text();
            String pic = div.select("img").attr("src");
            String tag = div.select(".tag").text();
            String remark = div.select("div.bottom > div").get(0).text();
            Vod vod = new Vod(id, name, pic, tag + (remark.isBlank() ? "" : "/") + remark);
            list.add(vod);
        }
        return Result.string(list);
    }
}

